package com.module.regrole;

import java.util.ArrayList;
import java.util.List;

import org.phprpc.util.PHPSerializer;
import org.xsocket.connection.INonBlockingConnection;

import com.command.Group;
import com.connect.agreement.Packet;
import com.game.base.BaseSimulation;
import com.game.base.SimulationObject;
import com.game.database.MysqlHelper;
import com.game.manager.CommManager;
import com.game.manager.SobManager;
import com.game.performers.Actor;
import com.server.User;

public class RegRoleManager {
	public static final byte GetRoleList = 1;
	public static final byte CreateRole = 2;
	public static final byte DeleteRole = 3;
	public static final byte Failed = 4;
	public static final byte EnterGame = 5;
	public static final byte ReturnToSelect = 6;
	public static final byte GetRoleId = 7;
	
	public static List<Actor> getRoleList(User user,INonBlockingConnection nbc){
		String[] filters = new String[1];
		filters[0] = Actor.class.getName();
		ArrayList<Actor> ret = new ArrayList<Actor>();
		if(user.mirror>0){
			List<SimulationObject> list = SobManager.get().getChilds(user.mirror, filters);			
			if(list != null){
				for(SimulationObject obj : list){
					ret.add((Actor) obj);
				}	
			}
		}
		if(nbc!=null){
			try {
				Packet p = new Packet();
				p.setBuffer(Group.RegRole, RegRoleManager.GetRoleList, new PHPSerializer().serialize(ret),true);
				nbc.write(p.byteBuffer());			
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	public static boolean checkRoleName(String name){
		Object[] parms = new Object[1];
		parms[0] = name;
		try {
			Object nameret = MysqlHelper.ExecuteScalar("select `name` from role_name where `name` = ?", parms);			
			return name.equals(nameret);
		} catch (Exception e) {
			return false;
		}
	}
	
	public static int getRoleId(String name){
		Object[] parms = new Object[1];
		parms[0] = name;
		try {
			int id = (Integer)MysqlHelper.ExecuteScalar("select `roleid` from role_name where `name` = ?", parms);			
			return id;
		} catch (Exception e) {
		}
		return 0;
	}
	
	public static boolean createRole(User user,RegInfo info,INonBlockingConnection nbc){
		if(!checkRoleName(info.name)){
			SimulationObject usermirror = SobManager.get().getSob(user.mirror);
			if(usermirror.children.size()>=5){
				return false;
			}
			Actor actor = (Actor)SobManager.get().createSob(Actor.class.getName(), user.mirror);
			actor.setName(info.name);
			actor.sex = info.sex;
			actor.role = info.role;
			actor.wakeup();
			Object[] parms = new Object[2];
			parms[0] = actor.id;
			parms[1] = actor.getName();
			try {
				MysqlHelper.ExecuteInsert("insert into role_name (`roleid`,`name`) values (?,?)", parms);
			} catch (Exception e) {
				
			}
			SobManager.get().updateSob(actor);
			return true;
		} else {
			try {
				if(nbc != null){
					Packet packet = new Packet();
					packet.setBuffer(Group.RegRole, Failed, new PHPSerializer().serialize(info.name+" 已经存在"),true);
					nbc.write(packet.byteBuffer());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}
	}
	
	public static boolean delete(int id, INonBlockingConnection nbc){
		SimulationObject obj = SobManager.get().getSob(id);
		if(obj instanceof Actor){
			Actor actor = (Actor)obj;
			
		}
		return false;
	}
	
	public static boolean enterGame(User user,int id,INonBlockingConnection nbc){
		user.ctrl = id;
		user.updateAttribute("ctrl");
		Actor role = (Actor)SobManager.get().getSob(id);
		CommManager.get().subscribe(nbc, role);
		return true;
	}
	
	public static boolean exitGame(User user,int id,INonBlockingConnection nbc){
		user.ctrl = 0;
		user.updateAttribute("ctrl");
		CommManager.get().unSubscribe(nbc);
		return true;
	}
	
	public static void update(INonBlockingConnection nbc,Packet packet){
		if(packet == null)
			return;
		User user = BaseSimulation.get().onlineusers.get(nbc.getId());
		Packet retpacket;
		int id;
		try {
			switch(packet.getCmd()){
			case GetRoleList://获得角色列表
				if(user != null){
					getRoleList(user,nbc);
				}
				break;
			case CreateRole:
				RegInfo info = (RegInfo)new PHPSerializer().unserialize(packet.getContent());
				if(createRole(user,info,nbc)){
					getRoleList(user,nbc);	
				}
				else
				{
					retpacket = new Packet();
					retpacket.setBuffer(Group.RegRole, Failed, new PHPSerializer().serialize("创建角色失败"),true);
					nbc.write(retpacket.byteBuffer());
				}
				break;
			case DeleteRole:
				id = (Integer)new PHPSerializer().unserialize(packet.getContent());
				if(delete(id, nbc)){
					getRoleList(user,nbc);	
				}
				else
				{
					retpacket = new Packet();
					retpacket.setBuffer(Group.RegRole, Failed, new PHPSerializer().serialize("删除角色失败"),true);
					nbc.write(retpacket.byteBuffer());
				}
				break;
			case EnterGame:
				id = (Integer)new PHPSerializer().unserialize(packet.getContent());
				if(enterGame(user, id, nbc)){
					packet.setBuffer(packet.getGroupcmd(), packet.getCmd(), new PHPSerializer().serialize(SobManager.get().getSob(id)),true);
					nbc.write(packet.byteBuffer());
				}
				else
				{
					retpacket = new Packet();
					retpacket.setBuffer(Group.RegRole, Failed, new PHPSerializer().serialize("进入游戏失败"),true);
					nbc.write(retpacket.byteBuffer());
				}
				break;
			case ReturnToSelect:
				id = (Integer)new PHPSerializer().unserialize(packet.getContent());
				if(id != user.ctrl){
					retpacket = new Packet();
					retpacket.setBuffer(Group.RegRole, Failed, new PHPSerializer().serialize("当前角色脱离你的控制"),true);
					nbc.write(retpacket.byteBuffer());
				}
				else if(exitGame(user, id, nbc)){
					nbc.write(packet.byteBuffer());
				}
				else
				{
					retpacket = new Packet();
					retpacket.setBuffer(Group.RegRole, Failed, new PHPSerializer().serialize("离开游戏失败"),true);
					nbc.write(retpacket.byteBuffer());
				}
				break;
			case GetRoleId:
				String rolename = (String)(new PHPSerializer().unserialize(packet.getContent()));
				id = getRoleId(rolename);
				retpacket = new Packet();
				retpacket.setBuffer(Group.RegRole, GetRoleId, new PHPSerializer().serialize(id),true);
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
